Asynchronous Method Invocation
   HOME

TheInfoList



OR:

In multithreaded
computer programming Computer programming or coding is the composition of sequences of instructions, called computer program, programs, that computers can follow to perform tasks. It involves designing and implementing algorithms, step-by-step specifications of proc ...
, asynchronous method invocation (AMI), also known as asynchronous method calls or the asynchronous pattern is a
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
in which the call site is not blocked while waiting for the called code to finish. Instead, the calling thread is notified when the reply arrives. Polling for a reply is an undesired option.


Background

AMI is a
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" ...
for
asynchronous Asynchrony is any dynamic far from synchronization. If and as parts of an asynchronous system become more synchronized, those parts or even the whole system can be said to be in sync. Asynchrony or asynchronous may refer to: Electronics and com ...
invocation of potentially long-running methods of an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an a ...
. It is equivalent to the IOU ("I owe you") pattern described in 1996 by Allan Vermeulen. In most programming languages a called method is executed synchronously, i.e. in the thread of execution from which it is invoked. If the method takes a long time to complete, e.g. because it is loading data over the internet, the calling thread is blocked until the method has finished. When this is not desired, it is possible to start a "worker thread" and invoke the method from there. In most programming environments this requires many lines of code, especially if care is taken to avoid the overhead that may be caused by creating many threads. AMI solves this problem in that it augments a potentially long-running ("synchronous") object method with an "asynchronous" variant that returns immediately, along with additional methods that make it easy to receive notification of completion, or to wait for completion at a later time. One common use of AMI is in the active object design pattern. Alternatives are synchronous method invocation and future objects. An example for an application that may make use of AMI is a web browser that needs to display a web page even before all images are loaded. Since
method Method (, methodos, from μετά/meta "in pursuit or quest of" + ὁδός/hodos "a method, system; a way or manner" of doing, saying, etc.), literally means a pursuit of knowledge, investigation, mode of prosecuting such inquiry, or system. In re ...
is a special case of procedure, asynchronous method invocation is a special case of
asynchronous procedure call An asynchronous procedure call (APC) is a unit of work in a computer. Definition Procedure calls can be synchronous or asynchronous. Synchronous procedure calls are made on one thread in a series, with each call waiting for the prior call to ...
.


Implementations


Java class

FutureTask class in
Java Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
use
events Event may refer to: Gatherings of people * Ceremony, an event of ritual significance, performed on a special occasion * Convention (meeting), a gathering of individuals engaged in some common interest * Event management, the organization of eve ...
to solve the same problem. This pattern is a variant of AMI whose implementation carries more overhead, but it is useful for objects representing
software components Component-based software engineering (CBSE), also called component-based development (CBD), is a style of software engineering that aims to construct a software system from components that are loosely-coupled and reusable. This emphasizes the sep ...
.


.NET Framework

* Asynchronous Programming Model (APM) pattern (used before .NET Framework 2.0) * Event-based Asynchronous Pattern (EAP) (used in .NET Framework 2.0) * Task-based Asynchronous Pattern (TAP) (used in .NET Framework 4.0)


Example

The following example is loosely based on a standard AMI style used in the .NET Framework. Given a method Accomplish, one adds two new methods BeginAccomplish and EndAccomplish: class Example Upon calling BeginAccomplish, the client immediately receives an object of type AsyncResult (which implements the IAsyncResult interface), so it can continue the calling thread with unrelated work. In the simplest case, eventually there is no more such work, and the client calls EndAccomplish (passing the previously received object), which blocks until the method has completed and the result is available. The AsyncResult object normally provides at least a method that allows the client to query whether the long-running method has already completed: interface IAsyncResult One can also pass a callback method to BeginAccomplish, to be invoked when the long-running method completes. It typically calls EndAccomplish to obtain the return value of the long-running method. A problem with the callback mechanism is that the callback function is naturally executed in the worker thread (rather than in the original calling thread), which may cause race conditions. In the .NET Framework documentation, the term event-based asynchronous pattern refers to an alternative API style (available since .NET 2.0) using a method named AccomplishAsync instead of BeginAccomplish. A superficial difference is that in this style the return value of the long-running method is passed directly to the callback method. Much more importantly, the API uses a special mechanism to run the callback method (which resides in an event object of type AccomplishCompleted) in the same thread in which BeginAccomplish was called. This eliminates the danger of race conditions, making the API easier to use and suitable for software components; on the other hand this implementation of the pattern comes with additional object creation and synchronization overhead.


References


Further reading

*
Using asynchronous method calls in C#
{{Design Patterns patterns Threads (computing) Software design patterns